package com.zx._03_并发.chapter_04_public;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j;

/**
 * 对象发布——安全发布对象——单例模式4——枚举模式是线程安全的方式）
 */
@Slf4j
public class Demo_03_singleton4 {

    // 私有构造方法
    private Demo_03_singleton4() {
    }

    // 静态工厂方法
    public static Demo_03_singleton4 getInstance() {
        return Singleton.instance.getDemo();
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 20; i++) {
            pool.execute(() -> log.info(Demo_03_singleton4.getInstance().toString()));
        }
        pool.shutdown();
    }

    private enum Singleton {
        instance;
        private Demo_03_singleton4 demo;

        private Singleton() {
            demo = new Demo_03_singleton4();
        }

        public Demo_03_singleton4 getDemo() {
            return demo;
        }
    }
}
